#include <map>
#include <set>
#include <iostream>

using std::cout;
using std::endl;
using std::map;
using std::set;
using std::pair;
using std::string;

void test0()
{
    //map中存放的元素类型是pair
    //
    //map中只能存放关键字相同的元素
    //默认情况下按升序进行排列
    //
    //底层实现依然是红黑树
    map<string, string> cities{
        {"027", "武汉"},
        {"0755", "深圳"},
        {"021", "上海"},
        {"010", "北京"},
        {"021", "广州"}
    };

    cout << "cities' size:" << cities.size() << endl;
    cout << "cities's empty:" << cities.empty() << endl;

    for(auto & e : cities) {
        cout << e.first << " --> " << e.second << endl;   
    }
    cout << endl;

    //通过下标运算符来访问, 获取key对应的value的值
    //1. 如果key存在，就直接返回相应的value
    cout << "cities[021]:" << cities["021"] << endl;
    //2. 如果key不存在，就直接添加一条新记录, value值是默认的
    cout << "cities[022]:" << cities["022"] << endl;
    cout << "cities' size:" << cities.size() << endl;

    cities["022"] = "天津";//3. 更新value的值

    for(auto & e : cities) {
        cout << e.first << " --> " << e.second << endl;   
    }
    cout << endl;
    
    //元素的添加,需要使用insert
    //pair<map<string,string>::iterator, bool> ret = 
    //auto ret = cities.insert(pair<string,string>("023", "重庆"));
    auto ret = cities.insert(pair<string,string>("027", "武汉"));
    if(ret.second == true) {
        cout << "添加成功" << endl;
        cout << ret.first->first << " --> " 
             << ret.first->second << endl;
    } else {
        cout << "添加失败" << endl;
        cout << ret.first->first << " --> " 
             << ret.first->second << endl;
    }
}

void test1()
{
    //容器之间可以混合使用
    map<string, set<int>> m;
    //m.insert("027")
    m["027"].insert(1);
    m["027"].insert(2);
    m["022"].insert(10);
    m["022"].insert(12);

    for(auto & e : m) {
        cout << e.first << " ";
        for(auto & n : e.second) {
            cout << n << " ";
        }
        cout << endl;
    }
}

int main()
{
    /* test0(); */
    test1();

    return 0;
}

